package com.isoft.mapper;

import com.isoft.entity.DfmeCkoeEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeCkoeMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "msgId", keyColumn = "MSG_ID")
    void insertMeta(DfmeCkoeEntity entity);

    @Insert({"MERGE INTO FLIGHT_BASE fb " +
            "USING (SELECT #{dflt.flightId} AS FLIGHT_ID FROM DUAL) src " +
            "ON (fb.FLIGHT_ID = src.FLIGHT_ID) " +
            "WHEN MATCHED THEN " +
            "UPDATE SET " +
            "fb.FLIGHT_ATTRIBUTE = #{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "fb.FLIGHT_FILE_ID = #{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "fb.FLIGHT_IDENTIFIER = #{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "fb.STATUS = CASE WHEN #{dflt.status,jdbcType=VARCHAR} IN ('NORMAL', 'DELAYED', 'CANCELLED', 'BOARDING', 'DEPARTED', 'ALTERNATE', 'RETURNED', 'BOARDING_COMPLETED') " +
            "THEN #{dflt.status,jdbcType=VARCHAR} ELSE 'NORMAL' END, " +
            "fb.INTERNAL_STATUS = #{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "fb.MANUAL_STATUS = #{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "fb.MANUAL_INTERNAL_STATUS = #{dflt.manualInternalStatus,jdbcType=VARCHAR}, " +
            "fb.RELEASE_TIME = TO_TIMESTAMP(#{dflt.flightCreateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss') " +
            "WHEN NOT MATCHED THEN " +
            "INSERT (FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, " +
            "STATUS, INTERNAL_STATUS, MANUAL_STATUS, MANUAL_INTERNAL_STATUS, RELEASE_TIME) " +
            "VALUES (" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "CASE WHEN #{dflt.status,jdbcType=VARCHAR} IN ('NORMAL', 'DELAYED', 'CANCELLED', 'BOARDING', 'DEPARTED', 'ALTERNATE', 'RETURNED', 'BOARDING_COMPLETED') " +
            "THEN #{dflt.status,jdbcType=VARCHAR} ELSE 'NORMAL' END, " +
            "#{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR}, " +
            "TO_TIMESTAMP(#{dflt.flightCreateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))"})
    int insertOrUpdateFlightBase(DfmeCkoeEntity entity);

    @Insert("INSERT INTO CHECKIN_COUNTER(COUNTER_ID, FLIGHT_ID, COUNTER_NUMBER, START_TIME, END_TIME, COUNTER_TYPE) " +
            "VALUES(SEQ_COUNTER.nextval, " +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "'CKOE', " +
            "TO_TIMESTAMP(#{dflt.flightCreateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "NULL, " +
            "'F')")
    int insertCheckinCounter(DfmeCkoeEntity entity);
}